///////////////////////////////////////////////////////////////
// File: "CProfileManager.cpp"
// Author: Ryan Burkevich (RB)
// Purpose: To contain and manage all of the profiles.
///////////////////////////////////////////////////////////////

#include "CProfileManager.h"
#include "CBitmapFontEngine.h"
#include "../xml/tinyxml.h"

CProfileManager::CProfileManager()
{
	m_nNumberOfProfiles = 0;

	tFastTimes tFastTimes;
	tFastTimes.fTime = 300;
	sprintf_s(tFastTimes.szProfileName, "Blue Smoke");

	for(int i = 1; i < 11; i++)
	{
		sprintf_s(tFastTimes.szLevel, "Level%i", i);
		m_vFastestTimes.push_back(tFastTimes);
	}
}

CProfileManager::~CProfileManager()
{

}

void CProfileManager::AddProfile(CProfile* pProfile)
{
	if (pProfile == NULL)
		return;

	if(m_nNumberOfProfiles >= 5)
		return;

	pProfile->InitItems();

	m_vProfileList.push_back(pProfile);

	pProfile->AddRef();
	
	m_nNumberOfProfiles++;
}

void CProfileManager::RemoveProfile(CProfile* pProfile)
{
	if (pProfile == NULL)
		return;

	for (vector<CProfile*>::iterator iter = m_vProfileList.begin(); iter != m_vProfileList.end(); iter++)
	{
		if ((*iter) == pProfile)
		{
			(*iter)->Release();
			iter = m_vProfileList.erase(iter);
			break;
		}
	}
	
	if(m_nNumberOfProfiles == 0)
		return;
	
	m_nNumberOfProfiles--;
}

void CProfileManager::RemoveAllProfiles()
{
	for(unsigned int i = 0; i < m_vProfileList.size(); i++)
	{
		for(int j = 0; j < 3; j++)
		{
			m_vProfileList[i]->SetCurrentItem(j);
			CSGD_TextureManager::GetInstance()->ReleaseTexture(m_vProfileList[i]->GetCurrentItem().nItemID);
		}
		m_vProfileList[i]->Release();
	}
	m_vProfileList.clear();
}

bool CProfileManager::LoadProfiles(const char* szFileName)
{
	TiXmlDocument xmlDoc;

	if(!xmlDoc.LoadFile(szFileName))
		return false;
	
	int nNumProfiles = 0;

	TiXmlElement* pRoot = xmlDoc.RootElement();
	pRoot->Attribute("NumberOfProfiles", &nNumProfiles);
	
	TiXmlElement* pElement1 = pRoot->FirstChildElement("Profile");

	for(int i = 0; i < nNumProfiles; i++)
	{
		CProfile* pProfile = new CProfile();
		char szTrophies[25];
		char szTrophyLevels[25];
		int nLevel = 0, nScore = 0, nColor = 0, nPwrUp = 0, nTrophy = 0;

		TiXmlElement* pElement2	= pElement1->FirstChildElement("Name");
		pProfile->SetProfileName(pElement2->Attribute("ProfileName")); 

		TiXmlElement* pElement3	= pElement2->NextSiblingElement("Level");
		pElement3->Attribute("CurrentLevel", &nLevel);
		pProfile->SetCurrentLevel(nLevel);

		TiXmlElement* pElement4	= pElement3->NextSiblingElement("Score");
		pElement4->Attribute("Score", &nScore);
		pProfile->SetScore((unsigned int)nScore);

		TiXmlElement* pElement5 = pElement4->NextSiblingElement("Color");
		pElement5->Attribute("Color", &nColor);
		pProfile->SetColor(nColor);

		TiXmlElement* pElement6 = pElement5->NextSiblingElement("PowerUps");
		for(int i = 0; i < 7; i++)
		{
			pElement6->Attribute(pProfile->GetPwrUpType(i), &nPwrUp);
			if(nPwrUp == 1)
				pProfile->AddPwrUpToList(i);
		}

		TiXmlElement* pElement7 = pElement6->NextSiblingElement("Level1Trophies");
		for(int i = 1; i < 11; i++)
		{
			if(i != 1)
			{
				sprintf_s(szTrophyLevels, "Level%iTrophies", i);
				pElement7 = pElement7->NextSiblingElement(szTrophyLevels);
			}	

			for(int j = 1; j < 4; j++)
			{
				sprintf_s(szTrophies, "Trophy%i", j);
				switch(j)
				{
				case 1:
					pElement7->Attribute(szTrophies, &nTrophy);
					if(nTrophy == 1)
						pProfile->AddTrophy(true, false, false, i);
					break;
				case 2:
					pElement7->Attribute(szTrophies, &nTrophy);
					if(nTrophy == 1)
						pProfile->AddTrophy(false, true, false, i);
					break;
				case 3:
					pElement7->Attribute(szTrophies, &nTrophy);
					if(nTrophy == 1)
						pProfile->AddTrophy(false, false, true, i);
					break;
				}
			}
		}

		AddProfile(pProfile);
		pProfile->Release();
		pElement1 = pElement1->NextSiblingElement("Profile");
	}

	return true;
}

bool CProfileManager::SaveProfiles(const char* szFileName)
{
	TiXmlDocument xmlDoc;

	TiXmlElement* pStartElement = new TiXmlElement("ProfileList");
	pStartElement->SetAttribute("NumberOfProfiles", GetNumberOfProfiles());
	xmlDoc.LinkEndChild(pStartElement);

	for(unsigned int i = 0; i < m_vProfileList.size(); i++)
	{
		TiXmlElement* pElement1 = new TiXmlElement("Profile");
		pStartElement->LinkEndChild(pElement1);

		TiXmlElement* pElement2 = new TiXmlElement("Name");
		pElement2->SetAttribute("ProfileName", m_vProfileList[i]->GetProfileName());
		pElement1->LinkEndChild(pElement2);

		TiXmlElement* pElement3 = new TiXmlElement("Level");
		pElement3->SetAttribute("CurrentLevel", m_vProfileList[i]->GetCurrentLevel());
		pElement1->LinkEndChild(pElement3);

		TiXmlElement* pElement4 = new TiXmlElement("Score");
		pElement4->SetAttribute("Score", m_vProfileList[i]->GetScore());
		pElement1->LinkEndChild(pElement4);

		TiXmlElement* pElement5 = new TiXmlElement("Color");
		pElement5->SetAttribute("Color", m_vProfileList[i]->GetINTColor());
		pElement1->LinkEndChild(pElement5);
		
		TiXmlElement* pElement6 = new TiXmlElement("PowerUps");

		int	nPwrUp = 0;
		char* szPwrUpType;

		for(int j = 0; j < 7; j++)
		{
			szPwrUpType = m_vProfileList[i]->GetPwrUpType(j);

			if(m_vProfileList[i]->CheckForPwrUp(j))
				nPwrUp = 1;//true
			else
				nPwrUp = 0;//false

			pElement6->SetAttribute(szPwrUpType, nPwrUp);
		}
		pElement1->LinkEndChild(pElement6);

		char szTrophies[25];
		char* szTrophyLevel;

		for(int j = 1; j < 11; j++)
		{

			szTrophyLevel = m_vProfileList[i]->GetTrophyLevel(j);
			TiXmlElement* pElement7 = new TiXmlElement(szTrophyLevel);

			for(int k = 1; k < 4; k++)
			{	
				sprintf_s(szTrophies, "Trophy%i", k);
				
				if(m_vProfileList[i]->CheckForTrophy(k, j))
				{
					pElement7->SetAttribute(szTrophies, 1);//true
				}
				else
					pElement7->SetAttribute(szTrophies, 0);//false
			}
			pElement1->LinkEndChild(pElement7);
		}
	}

	if(!xmlDoc.SaveFile(szFileName))
		return false;

	return true;
}

void CProfileManager::SetCurrentProfile(int nIndex)
{
	if(nIndex == -1)
		return;
	m_pCurrentProfile = m_vProfileList[nIndex];
}
	
void CProfileManager::RenderProfile(int nPosX, int nPosY, int nIndex, DWORD dwColor)
{
	if(nIndex == -1)
		return;

	CBitmapFontEngine* pBFE = CBitmapFontEngine::GetInstance();
	m_nFontID = pBFE->LoadFont("BS_TechDemoTitle.xml");
	pBFE->RenderString(m_vProfileList[nIndex]->GetProfileName(), m_nFontID, 0, dwColor, nPosX, nPosY, 0);
	
}

void CProfileManager::RenderScore(int nPosX, int nPosY, int nIndex , DWORD dwColor )
{
	CBitmapFontEngine* pBFE = CBitmapFontEngine::GetInstance();
	m_nFontID = pBFE->LoadFont("BS_TechDemoTitle.xml");
	char szBuffer[100];
	sprintf_s(szBuffer, "%i", m_vProfileList[nIndex]->GetScore());
	pBFE->RenderString(szBuffer, m_nFontID, 0, dwColor, nPosX, nPosY, 0);

}

void CProfileManager::ShutdownProfileManager()
{
	SaveProfiles("BS_Profiles.xml");
	SaveTimes("BS_LevelTimes.xml");

	RemoveAllProfiles();
}

void CProfileManager::NewFastestTime(float fTime)
{
	int nLevel = GetCurrentProfile()->GetWhichLevelPlaying();

	if(fTime < m_vFastestTimes[nLevel-1].fTime)
	{
		for(int i = 0; i < m_nNumberOfProfiles; i++)
		{
			if(strcmp(m_vProfileList[i]->GetProfileName(), m_vFastestTimes[nLevel-1].szProfileName) == 0)
			{
				m_vProfileList[i]->RemoveTimeTrophy(nLevel);
			}
		}
		m_vFastestTimes[nLevel-1].fTime = fTime;
		strcpy_s(m_vFastestTimes[nLevel-1].szProfileName, 25, GetCurrentProfile()->GetProfileName());
		GetCurrentProfile()->AddTrophy(true);
	}
}

bool CProfileManager::SaveTimes(const char* szFileName)
{
	TiXmlDocument xmlDoc;

	TiXmlElement* pStartElement = new TiXmlElement("FastestTimes");
	xmlDoc.LinkEndChild(pStartElement);
	
	for(int i = 1; i < 11; i++)
	{
		TiXmlElement* pElement1 = new TiXmlElement(GetFastestTimeLevel(i));
		pElement1->SetAttribute("Name", GetFastestTimeName(i));
		pElement1->SetAttribute("Time", (int)GetFastestTime(i));
		pStartElement->LinkEndChild(pElement1);
	}

	if(!xmlDoc.SaveFile(szFileName))
		return false;

	return true;
}

bool CProfileManager::LoadTimes(const char* szFileName)
{
	TiXmlDocument xmlDoc;

	if(!xmlDoc.LoadFile(szFileName))
		return false;
	
	TiXmlElement* pRoot = xmlDoc.RootElement();
	TiXmlElement* pElement1 = pRoot->FirstChildElement("Level1");
	
	char szLevels[25];
	int nTime = 0;

	for(int i = 1; i < 11; i++)
	{
		if(i != 1)
		{
			sprintf_s(szLevels, "Level%i", i);
			pElement1 = pElement1->NextSiblingElement(szLevels);
		}

		strcpy_s(m_vFastestTimes[i-1].szProfileName, 25, pElement1->Attribute("Name"));
		pElement1->Attribute("Time", &nTime);
		m_vFastestTimes[i-1].fTime = (float)nTime;
	}

	return true;
}
